package com.lkm.app_ui.ui;

import com.lkm.frame.P;
import com.lkm.market2_0.help.RandomStringUtils;
import com.lkm.market2_0.m_imple.steelyardserver.server.Config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;



public class Main {

    static Socket SCsocket;


    private static OutputStream outputStream;
    private static InputStream inputStream;

    public static void bin22() {

        try {
            

            Process su;
            try {
                su = Runtime.getRuntime().exec("/system/bin/su");
            } catch (Exception e) {
                su = Runtime.getRuntime().exec("su");
            }

            outputStream = su.getOutputStream();//.write(cmd.getBytes());
            inputStream = su.getInputStream();

        } catch (Exception e) {
            e.printStackTrace();
        }


        
        new Thread(new Runnable() {
            @Override
            public void run() {




//                        socket.connect(new InetSocketAddress("127.0.0.1", 22));












                byte[] bytes = new byte[255];
                while (true) {
                    try {
                        int size = inputStream.read(bytes);
                        if (size != -1 && SCsocket != null)
                            SCsocket.getOutputStream().write(bytes, 0, size);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        
        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    try {
                        SCsocket = new Socket();
                        SCsocket.connect(new InetSocketAddress("127.0.0.1", 47118));
                        break;
                    } catch (Exception e) {

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                }

                byte[] bytes = new byte[255];
                while (true) {
                    try {

                        int size = SCsocket.getInputStream().read(bytes);
                        P.p("====su 读到数据"+size);
                        if (size != -1 && outputStream != null)
                            outputStream.write(bytes, 0, size);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


    }

    public static void main(String[] s) throws IOException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                runServer();
            }
        }).start();



        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

                while (true) {
                    try {
                        String res = bufferedReader.readLine()+"\n";
                        if (scSocket != null) {
                            scSocket.getOutputStream().write(res.getBytes("utf-8"));
                        }
                    } catch (Exception e) {
                    }

                }
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {


                while (true) {






                    if(scSocket==null){
                        try {
                            Thread.sleep(1000);
                        }catch (Exception e){};
                    }

                    byte[] bytes = new byte[255];

                    while (true) {
                        try {
                            int size = scSocket.getInputStream().read(bytes);
                            System.out.write(bytes,0,size);
                        } catch (Exception e) {
                            break;
                        }

                    }
                }
            }
        }).start();


    }

    static ServerSocket server47118 = null;
    static Socket scSocket;

    
    private static void runServer() {

        while (true) {
            try {
                server47118 = new ServerSocket(47118);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (server47118 == null) {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }

        P.p("==== ");
        while (true) {
            try {
                
                P.p("==== 接待 -=");
                Socket socket = server47118.accept();

                try {
                    socket.setSoTimeout(Config.steelyardActionOutTime);
                } catch (Exception e) {

                }
                socket.setKeepAlive(true);

                scSocket = socket;
                try {
                    
                    InetAddress inetAddress = socket.getInetAddress();

                    

                    String clientSessionId = inetAddress.getHostAddress() + socket.getPort() + RandomStringUtils.random(6, RandomStringUtils.azAZ90);


//                        steelyardMac=inetAddress.getHostAddress()+socket.getPort();

//                        steelyardMac=NetHelp.findLANCommunicationMacByIpForLinux(inetAddress.getHostAddress());


                    if (P.isDebug()) {
                        P.p("====ip mac " + inetAddress.getHostAddress() + "   " + clientSessionId);

//                            UIP.p("接待新机： session：" + clientSessionId + "  IP地址：" + inetAddress.getHostAddress(), clientSessionId);

                    }


                } catch (Exception e) {

                    e.printStackTrace();
                }

            } catch (IOException e) {
            }
        }

    }
}
